package com.exam.controller;

import com.exam.domain.Field;
import com.exam.domain.KnowledgePoint;
import com.exam.domain.Question;
import com.exam.domain.QuestionType;
import com.exam.service.interfaces.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jly on 2017/2/13.
 */
@Controller
@RequestMapping("question")
public class QuestionController {
    @Resource
    private QuestionService questionService;

    /**
     * 进入试题列表界面
     *
     * @return: 进入试题列表界面
     * @since: 2017-2-13
     * @author: jly
     */
    @RequestMapping("/questionLists")
    public String userList(HttpSession session, HttpServletRequest request) {
        if (session.getAttribute("userName") == null) {
            return "redirect:" + UserController.basePath(request);
        }
        return "question/questionList";
    }

    /**
     * 查询所有科目
     *
     * @return: 返回数据库科目表所有内容
     * @Since: 2017-2-13
     * @author: jly
     */
    @RequestMapping("/fields")
    @ResponseBody
    public List<Field> allFields() {
        return questionService.queryFields();
    }

    /**
     * 查询所有试题类型
     *
     * @return: 返回试题类型所有内容
     * @Since: 2017-2-13
     * @author: jly
     */
    @RequestMapping("/questionTypes")
    @ResponseBody
    public List<QuestionType> allQuestionTypes() {
        return questionService.queryQuestionType();
    }

    /**
     * 查询所有知识点
     *
     * @return: 返回知识点所有内容
     * @Since: 2017-2-13
     * @author: jly
     */
    @RequestMapping("questionPoints")
    @ResponseBody
    public List<KnowledgePoint> allyPoints() {
        return questionService.queryPoint();
    }

    /**
     * 查看试题列表具体数据
     *
     * @return: 返回试题列表及相关信息
     * @since: 2017-2-13
     * @author: jly
     */
    @RequestMapping("/List")
    @ResponseBody
    public List<Question> tabList(String creator, Integer typeId, Integer kindId, Integer pointId, HttpSession session) {
        Integer roleId = (Integer) session.getAttribute("roleId");
        Long userId = (Long) session.getAttribute("userId");
        if (roleId == 2) {
            List<Question> questionLists = questionService.questionList_teachet(creator, typeId, kindId, pointId, userId);
            return questionLists;
        } else {
            List<Question> questionLists = questionService.questionLists(creator, typeId, kindId, pointId);
            return questionLists;
        }
    }

    /**
     * 删除试题
     *
     * @param questionid:试题id
     * @return 返回是否删除成功
     * @author jly
     * @since 2017-2-24
     */
    @RequestMapping("/delQuestion")
    @ResponseBody
    public boolean delQuestion(int questionid) {
        if ((questionService.delAnswer(questionid)) || (questionService.delQuestion(questionid))) {
            return true;
        }
        return false;
    }

    /**
     * 加载编辑框信息
     *
     * @param questionid:试题id
     * @return id对应的试题信息
     * @author jly
     * @since 2017-2-24
     */
    @RequestMapping("/loadEdit")
    @ResponseBody
    public Question loadEdit(int questionid) {
        return questionService.loadEdit(questionid);
    }

    /**
     * 查看试题详细信息
     *
     * @param questionId 试题id
     * @return: 返回试题详细信息页面
     * @since: 2017-2-24
     * @author: jly
     */
    @RequestMapping("/questionDetail/{questionId}")
    public String questionDetailView(@PathVariable(value = "questionId") Integer questionId,
                                     Model model, HttpSession session, HttpServletRequest request) {
        //    System.out.println(questionId + "=====================================================================================");
        if (session.getAttribute("userName") == null) {
            return "redirect:" + UserController.basePath(request);
        } else {
            Question question = questionService.questionDetail(questionId);
            model.addAttribute("question", question);
            String answeroption = "";
            if (question.getAnswer() == null) {
                return "question/questiondetail";
            } else {
                answeroption = question.getAnswer().getAnswerOption();
                if (answeroption.indexOf("A") != -1) {
                    return "question/questiondetail";
                } else {
                    return "question/questiondetail2";
                }
            }
        }
    }

    /**
     * 编辑试题信息
     *
     * @param questionId:问题id,fielsId:专业id，pointId:知识点id，answer:正确答案
     * @return: 修改试题信息
     * @since: 2017-2-24
     * @author: jly
     */
    @RequestMapping("/questionInfo")
    @ResponseBody
    public boolean questionInfo(int questionId, int fielsId, int pointId, String answer) {
        if (questionService.questionInfo(fielsId, pointId, answer, questionId)) {
            return true;
        }
        return false;
    }

    /**
     * 添加试题页面--老师
     *
     * @author jly
     * @since 2017-3-2
     */
    @RequestMapping("/addQuestion")
    public String addQuestion(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("userName") == null) {
            return "redirect:" + UserController.basePath(request);
        }
        return "question/addquestion";
    }

    /**
     * 新增试题
     *
     * @param questionName:试题名称， questionDesc：试题描述
     *                           kindId:科目
     *                           q_type：试题类型
     *                           pointId：知识点选择
     *                           pointname：知识点新增名称
     *                           pointdesc：知识点新增描述
     *                           answerA：答案A内容
     *                           answerB：答案B内容
     *                           answerC：答案C内容
     *                           answerD：答案D内容
     *                           trueanswer:答案T内容
     *                           falseanswer:答案F内容
     *                           select1:选择题正确答案
     *                           select2:判断题正确答案
     * @author jly
     * @since 2017-3-3
     */
    @RequestMapping("/addQueation")
    @ResponseBody
    public int addQuestion(String questionName, String questionDesc, Integer kindId, Integer q_type,
                           Integer pointId, String pointname, String pointdesc, String answerA,
                           String answerB, String answerC, String answerD, String trueanswer, String falseanswer,
                           String select1, String select2, String kindname, String kinddesc, String typename,
                           String select3, String level, HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        boolean addPoint;
        boolean addAnswer;
        boolean addKind = false;
        boolean addType = false;
        int newpointId;
        int newkindId;
        int newtypeId;
        int newquestionId;
        int result = 0;
        String answer4 = "";
        String answers1 = "A." + answerA + "; B." + answerB + "; C." + answerC + "; D." + answerD;
        String answers2 = "T." + trueanswer + "; F." + falseanswer;
        int Qcount = questionService.queryQuestionCount(questionName);
        if(Qcount > 0){
            return 2;
        }
        //新增科目
        if (kindId == 0) {
            addKind = questionService.addKind(kindname, kinddesc);
            newkindId = questionService.queryKindId(kindname);
            //新增类型
            if (q_type == 4) {
                addType = questionService.addType(typename);
                newtypeId = questionService.queryTypeId(typename);
                //新增知识点
                if (pointId == 0) {
                    addPoint = questionService.addPoint(pointname, pointdesc);
                    newpointId = questionService.queryPointId(pointname);
                    if (addKind && addType && addPoint) {
                        if (questionService.addQuestion(questionName, userId, newtypeId, answer4, newkindId, newpointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    } else {
                        result = 0;
                    }
                }
                //不是新增知识点
                else {
                    if (addKind && addType) {
                        if (questionService.addQuestion(questionName, userId, newtypeId, answer4, newkindId, pointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    } else {
                        result = 0;
                    }
                }
            }
            //不是新增类型
            else {
                if (q_type == 1) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addKind && addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select1, newkindId, newpointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers1, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (addKind) {
                            if (questionService.addQuestion(questionName, userId, q_type, select1, newkindId, pointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers1, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
                if (q_type == 2) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addKind && addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select2, newkindId, newpointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers2, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (addKind) {
                            if (questionService.addQuestion(questionName, userId, q_type, select2, newkindId, pointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers2, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
                if (q_type == 3) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addKind && addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select3, newkindId, newpointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (addKind) {
                            if (questionService.addQuestion(questionName, userId, q_type, select3, newkindId, pointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
                if ((q_type != 1) && (q_type != 2) && (q_type != 3)) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addKind && addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, answer4, newkindId, newpointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (addKind) {
                            if (questionService.addQuestion(questionName, userId, q_type, answer4, newkindId, pointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
            }
        }
        //不是新增科目
        else {
            //新增类型
            if (q_type == 4) {
                addType = questionService.addType(typename);
                newtypeId = questionService.queryTypeId(typename);
                //新增知识点
                if (pointId == 0) {
                    addPoint = questionService.addPoint(pointname, pointdesc);
                    newpointId = questionService.queryPointId(pointname);
                    if (addType && addPoint) {
                        if (questionService.addQuestion(questionName, userId, newtypeId, answer4, kindId, newpointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    } else {
                        result = 0;
                    }
                }
                //不是新增知识点
                else {
                    if (addType) {
                        if (questionService.addQuestion(questionName, userId, newtypeId, answer4, kindId, pointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    } else {
                        result = 0;
                    }
                }
            }
            //不是新增类型
            else {
                if (q_type == 1) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select1, kindId, newpointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers1, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (questionService.addQuestion(questionName, userId, q_type, select1, kindId, pointId, questionDesc, level)) {
                            newquestionId = questionService.queryQuestionId(questionName);
                            addAnswer = questionService.addAnswer2(answers1, newquestionId);
                            if (addAnswer) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
                if (q_type == 2) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select2, kindId, newpointId, questionDesc, level)) {
                                newquestionId = questionService.queryQuestionId(questionName);
                                addAnswer = questionService.addAnswer2(answers2, newquestionId);
                                if (addAnswer) {
                                    result = 1;
                                } else {
                                    result = 0;
                                }
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (questionService.addQuestion(questionName, userId, q_type, select2, kindId, pointId, questionDesc, level)) {
                            newquestionId = questionService.queryQuestionId(questionName);
                            addAnswer = questionService.addAnswer2(answers2, newquestionId);
                            if (addAnswer) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                }
                if (q_type == 3) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, select3, kindId, newpointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (questionService.addQuestion(questionName, userId, q_type, select3, kindId, pointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    }
                }
                if ((q_type != 1) && (q_type != 2) && (q_type != 3)) {
                    //新增知识点
                    if (pointId == 0) {
                        addPoint = questionService.addPoint(pointname, pointdesc);
                        newpointId = questionService.queryPointId(pointname);
                        if (addPoint) {
                            if (questionService.addQuestion(questionName, userId, q_type, answer4, kindId, newpointId, questionDesc, level)) {
                                result = 1;
                            } else {
                                result = 0;
                            }
                        } else {
                            result = 0;
                        }
                    }
                    //不是新增知识点
                    else {
                        if (questionService.addQuestion(questionName, userId, q_type, answer4, kindId, pointId, questionDesc, level)) {
                            result = 1;
                        } else {
                            result = 0;
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 进入组卷策略界面
     *
     * @author jly
     * @since 2017-3-14
     */
    @RequestMapping("/paperStrategies")
    public String paperStrategies(HttpServletRequest request) {
        if (request.getSession().getAttribute("userName") == null) {
            return "redirect:" + UserController.basePath(request);
        }
        return "question/paperstrategies";
    }

    /**
     * 把组卷策略存储到数据库
     *
     * @author jly
     * @since 2017-3-15
     */
    @RequestMapping("/addStrategies")
    @ResponseBody
    public int addStrategies(int fieldxz, int xzjd, int xzzd, int xzn, int pdjd,
                             int pdzd, int pdn, int tkjd, int tkzd, int tkn, String zgtValue) {
        int result;
        boolean xuanze1 = false;
        boolean xuanze2 = false;
        boolean xuanze3 = false;
        boolean panduan1 = false;
        boolean panduan2 = false;
        boolean panduan3 = false;
        boolean tiankong1 = false;
        boolean tiankong2 = false;
        boolean tiankong3 = false;
        boolean zhuguan1 = false;
        boolean zhuguan2 = false;
        boolean zhuguan3 = false;
        //一组主观题，直接分开
        String[] str = new String[4];
        //多组主观题，先分组
        String[] str2;
        if (questionService.queryFieldCount(fieldxz) > 0) {
            boolean del = questionService.delstrategies(fieldxz);
            if(del){
                xuanze1 = questionService.addXZJD(fieldxz, 1, xzjd, 1);
                xuanze2 = questionService.addXZZD(fieldxz, 1, xzzd, 2);
                xuanze3 = questionService.addXZN(fieldxz, 1, xzn, 3);
                panduan1 = questionService.addPDJD(fieldxz, 2, pdjd, 1);
                panduan2 = questionService.addPDZD(fieldxz, 2, pdzd, 2);
                panduan3 = questionService.addPDN(fieldxz, 2, pdn, 3);
                tiankong1 = questionService.addTKJD(fieldxz, 3, tkjd, 1);
                tiankong2 = questionService.addTKZD(fieldxz, 3, tkzd, 2);
                tiankong3 = questionService.addTKN(fieldxz, 3, tkn, 3);
                if (zgtValue.indexOf("&") == -1) {
                    str = zgtValue.split(",");
                    zhuguan1 = questionService.addZGJD(fieldxz, Integer.valueOf(str[0]),
                            Integer.valueOf(str[1]), 1);
                    zhuguan2 = questionService.addZGZD(fieldxz, Integer.valueOf(str[0]),
                            Integer.valueOf(str[2]), 2);
                    zhuguan3 = questionService.addZGN(fieldxz, Integer.valueOf(str[0]),
                            Integer.valueOf(str[3]), 3);
                } else {
                    String str3 = "";
                    str2 = zgtValue.split("&");
                    for (int i = 0; i < str2.length; i++) {
                        str = str2[i].split(",");
                        if (str[1].equals("0") && str[2].equals("0") && str[3].equals("0")) {
                            continue;
                        } else {
                            if (str3.equals(str[0])) {
                                zhuguan1 = questionService.updataZGJD(fieldxz, Integer.valueOf(str3),
                                        Integer.valueOf(str[1]), 1);
                                zhuguan2 = questionService.updataZGZD(fieldxz, Integer.valueOf(str3),
                                        Integer.valueOf(str[2]), 2);
                                zhuguan3 = questionService.updataZGN(fieldxz, Integer.valueOf(str3),
                                        Integer.valueOf(str[3]), 3);
                            } else {
                                zhuguan1 = questionService.addZGJD(fieldxz, Integer.valueOf(str[0]),
                                        Integer.valueOf(str[1]), 1);
                                zhuguan2 = questionService.addZGZD(fieldxz, Integer.valueOf(str[0]),
                                        Integer.valueOf(str[2]), 2);
                                zhuguan3 = questionService.addZGN(fieldxz, Integer.valueOf(str[0]),
                                        Integer.valueOf(str[3]), 3);
                            }
                            str3 = str[0];
                        }
                    }
                }
            }
        } else {
            xuanze1 = questionService.addXZJD(fieldxz, 1, xzjd, 1);
            xuanze2 = questionService.addXZZD(fieldxz, 1, xzzd, 2);
            xuanze3 = questionService.addXZN(fieldxz, 1, xzn, 3);
            panduan1 = questionService.addPDJD(fieldxz, 2, pdjd, 1);
            panduan2 = questionService.addPDZD(fieldxz, 2, pdzd, 2);
            panduan3 = questionService.addPDN(fieldxz, 2, pdn, 3);
            tiankong1 = questionService.addTKJD(fieldxz, 3, tkjd, 1);
            tiankong2 = questionService.addTKZD(fieldxz, 3, tkzd, 2);
            tiankong3 = questionService.addTKN(fieldxz, 3, tkn, 3);
            if (zgtValue.indexOf("&") == -1) {
                str = zgtValue.split(",");
                zhuguan1 = questionService.addZGJD(fieldxz, Integer.valueOf(str[0]),
                        Integer.valueOf(str[1]), 1);
                zhuguan2 = questionService.addZGZD(fieldxz, Integer.valueOf(str[0]),
                        Integer.valueOf(str[2]), 2);
                zhuguan3 = questionService.addZGN(fieldxz, Integer.valueOf(str[0]),
                        Integer.valueOf(str[3]), 3);
            } else {
                String str3 = "";
                str2 = zgtValue.split("&");
                for (int i = 0; i < str2.length; i++) {
                    str = str2[i].split(",");
                    if (str[1].equals("0") && str[2].equals("0") && str[3].equals("0")) {
                        continue;
                    } else {
                        if (str3.equals(str[0])) {
                            zhuguan1 = questionService.updataZGJD(fieldxz, Integer.valueOf(str3),
                                    Integer.valueOf(str[1]), 1);
                            zhuguan2 = questionService.updataZGZD(fieldxz, Integer.valueOf(str3),
                                    Integer.valueOf(str[2]), 2);
                            zhuguan3 = questionService.updataZGN(fieldxz, Integer.valueOf(str3),
                                    Integer.valueOf(str[3]), 3);
                        } else {
                            zhuguan1 = questionService.addZGJD(fieldxz, Integer.valueOf(str[0]),
                                    Integer.valueOf(str[1]), 1);
                            zhuguan2 = questionService.addZGZD(fieldxz, Integer.valueOf(str[0]),
                                    Integer.valueOf(str[2]), 2);
                            zhuguan3 = questionService.addZGN(fieldxz, Integer.valueOf(str[0]),
                                    Integer.valueOf(str[3]), 3);
                        }
                        str3 = str[0];
                    }
                }
            }
        }
        if (xuanze1 && xuanze2 && xuanze3 && panduan1 && panduan2 && panduan3
                && tiankong1 && tiankong2 && tiankong3 && zhuguan1 && zhuguan2 && zhuguan3) {
            result = 1;
        } else {
            result = 0;
        }
        return result;
    }
}
